package com.logic.modular.act.room;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comment.util.message.ResponseResult;
import com.logic.comment.util.JwtUtils;
import com.logic.modular.act.room.vo.RoomVo;
import com.comment.pojo.domain.logic.core.PvpMatch;
import com.comment.pojo.domain.logic.core.PvpRoom;
import com.logic.modular.game.room.mapper.PvpRoomMapper;
import com.logic.modular.game.room.service.PvpRoomServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Api(tags = "房间行为")
@Validated
@RestController
@RequestMapping("/RoomAct")
public class RoomAction {
    @Resource
    private PvpRoomServiceImpl pvpRoomService;
    @Resource
    private PvpRoomMapper pvpRoomMapper;

    @ApiOperation(value = "所有")
    @PostMapping("/allList")
    public ResponseResult<List<PvpRoom>> allList() {
        return ResponseResult.success(pvpRoomService.allList());
    }

    @ApiOperation(value = "查询")
    @PostMapping("/list")
    public ResponseResult<List<PvpRoom>> list(@RequestBody RoomVo.RoomAction.list vo) {
        LambdaQueryWrapper<PvpRoom> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PvpRoom::getRoomOwner, vo.getRoomOwner());
        wrapper.eq(PvpRoom::getMapId, vo.getMapId());
        wrapper.eq(PvpRoom::getMatchId, vo.getMatchId());
        wrapper.eq(PvpRoom::getGameType, vo.getGameType());
        return ResponseResult.success(pvpRoomMapper.selectList(wrapper));
    }

    @ApiOperation(value = "工具房间id查找")
    @PostMapping("/select")
    public ResponseResult<PvpRoom> select(@RequestBody RoomVo.RoomAction.select vo) {
        return ResponseResult.success(pvpRoomService.select(vo.getRoomOwner()));
    }

    @ApiOperation(value = "创建房间")
    @PostMapping("/createRoom")
    public ResponseResult<PvpRoom> createRoom(
            @RequestHeader String token,
            @RequestBody RoomVo.RoomAction.createRoom vo
    ) {
        return ResponseResult.success(pvpRoomService.createRoom(
                JwtUtils.getPlayerId(token), vo.getPassword(), vo.getMapId())
        );
    }

    @ApiOperation(value = "踢人")
    @PostMapping("/kickingPeople")
    public ResponseResult<PvpRoom> kickingPeople(
            @RequestHeader String token
            , @RequestBody RoomVo.RoomAction.operatingRoomLocation vo
    ) {
        return ResponseResult.success(pvpRoomService.kickingPeople(
                JwtUtils.getPlayerId(token), vo.getPos()
        ));
    }

    @ApiOperation(value = "关闭或打开座椅")
    @PostMapping("/closeOrOpenTheSeat")
    public ResponseResult<PvpRoom> closeOrOpenTheSeat(
            @RequestHeader String token
            , @RequestBody RoomVo.RoomAction.operatingRoomLocation vo
    ) {
        return ResponseResult.success(pvpRoomService.closeOrOpenTheSeat(
                JwtUtils.getPlayerId(token), vo.getPos()
        ));
    }

    @ApiOperation(value = "加入指定玩家房间")
    @PostMapping("/joinRoom")
    public ResponseResult<PvpRoom> joinRoom(
            @RequestHeader String token,
            @RequestBody RoomVo.RoomAction.joinRoom vo
    ) {
        return ResponseResult.success(pvpRoomService.joinRoom(
                JwtUtils.getPlayerId(token), vo.getRoomOwner(), vo.getPassword()
        ));
    }


    @ApiOperation(value = "离开房间")
    @PostMapping("/leaveRoom")
    public ResponseResult<PvpRoom> leaveRoom(
            @RequestHeader String token
    ) {
        return ResponseResult.success(pvpRoomService.leaveRoom(
                JwtUtils.getPlayerId(token)
        ));
    }

    @ApiOperation(value = "开始游戏")
    @PostMapping("/startGame")
    public ResponseResult<PvpMatch> startGame(
            @RequestHeader String token
    ) {
        return ResponseResult.success(pvpRoomService.startGame(
                JwtUtils.getPlayerId(token)
        ));
    }
}